<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="100px" label-suffix=":">
            <el-form-item label="仓库名称" prop="warehouse_id">
                <el-select v-model="model.warehouse_id" placeholder="请选择仓库" clearable size="small" style="width: 240px" :disabled="type == 'edit'">
                    <el-option v-for="(warehouse, index) in warehousesList" :key="index" :label="warehouse.name" :value="warehouse.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="仓库分区" prop="partition_id">
                <el-select v-model="model.partition_id" placeholder="请选择分区" clearable size="small" style="width: 240px" :disabled="type == 'edit'">
                    <el-option v-for="(dict, index) in dict.PartitionList" :key="index" :label="dict.name" :value="dict.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="货架编号" prop="name">
                <el-input v-model.trim="model.name" :disabled="type == 'edit'" placeholder="必填，仅支持字母、数字、中划线、下划线" clearable></el-input>
            </el-form-item>
            <el-form-item label="拣货权重" prop="weight">
                <el-input v-model.trim="model.weight" placeholder="非必填，仅支持数字" clearable></el-input>
            </el-form-item>
            <el-form-item label="备注说明" prop="remarks">
                <el-input type="textarea" v-model.trim="model.remarks" :rows="8" placeholder="非必填" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { goodsshelvesFind, goodsshelvesAdd, goodsshelvesEdit,getPartitionList,getWarehouseList } from '@/api/warehouse'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            model: {
                id: '',
                name: '',
                warehouse_id: 2,
                partition_id: 1,
            },
            rules: {
                
            },
            dict: {
                PartitionList: [],
                WarehouseList: []
            },
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}货架信息`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async mounted() {
        try {
            const [PartitionList, WarehouseList] = await Promise.all([getPartitionList(), getWarehouseList()])
            this.dict.PartitionList = PartitionList?.data?.list ?? []
            this.dict.WarehouseList = WarehouseList?.data?.list ?? []
            console.log(this.dict)
        } catch (error) {
            console.log(error)
        }
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            goodsshelvesFind(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    goodsshelvesEdit(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    goodsshelvesAdd(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
</style>
